package com.wwwc.index.web.servlet;

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;

import java.net.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import java.text.DecimalFormat;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;
import com.wwwc.index.web.ejb.*;

public class UserHomeManagerServlet extends HttpServlet {
    DecimalFormat td = new DecimalFormat("0.00");
    String addStorageCallBack = "1-0";

    public void doGet(HttpServletRequest request,  HttpServletResponse response)
	throws IOException, ServletException
    {
	doPost(request,response);
    }

    public void doPost(HttpServletRequest request,  HttpServletResponse response)
	throws IOException, ServletException
    {
        response.setContentType("text/html");

	HttpSession session = request.getSession(true);
        PrintWriter out     = response.getWriter();
	String user_ip      = request.getRemoteAddr();
	String call_path    = request.getContextPath()+request.getServletPath();

	String mid       = (String)request.getAttribute("mid");
	String action    = request.getParameter("aid");

	Hashtable dir_hs = (Hashtable)session.getAttribute("ContralOpenClose");
	if(dir_hs == null) {
	    return;
	}

	ResourceBundle SystemInfo = (ResourceBundle)session.getAttribute("SystemInfo");
	String user_home   = SystemInfo.getString("3wcenter.user.web.home");

	int image_size      = Integer.parseInt(SystemInfo.getString("3wcenter.default.post.upload.image.max_size"));
	int default_storage = Integer.parseInt(SystemInfo.getString("3wcenter.default.post.upload.image.total_size"));

	String user_name  = null;
	int user_age      = 0;
	int user_level    = 0;
	String user_fname = null;
	String user_since = null;
	String user_edu   = null;
	String user_zip   = null;
	String user_sex   = null;

	Hashtable user_info = (Hashtable)session.getAttribute("user_info");
	if(user_info == null) {
	    System.out.println(MyMessage.htmlMessage("UserHomeManagerServlet[101]"));
	    return;
	}

	user_name  = (String)user_info.get("user_name");
	user_age   = Integer.parseInt((String)user_info.get("user_age"));
	user_level = Integer.parseInt((String)user_info.get("user_level"));
	user_fname = (String)user_info.get("user_fname");
	user_since = (String)user_info.get("user_since");
	user_edu   = (String)user_info.get("user_edu");
	user_zip   = (String)user_info.get("user_zip");
	user_sex   = (String)user_info.get("user_sex");

	int sw = 1280;
	int sh = 1024;

	String sub_dir = request.getParameter("sub_dir");
	if(sub_dir == null || sub_dir.length()<1 || mid == null) {
	    return;
	}

	String table_name = request.getParameter("table_name");
	if(table_name == null || table_name.length()<1) {
	    return;
	}

	if(user_name == null || user_name.equals("guest")) {
	    return;
	}

	UserHomeBean ubean = new UserHomeBean();

	out.println(ubean.getManager(call_path, mid, sub_dir, user_name, user_level, default_storage));

	if(action == null) {
	    out.println(ubean.displayUserImage(sw, sh, request, mid, sub_dir, user_name, user_level));
	    return;
	}

	//=======================================================================
	// buy storage
	//=======================================================================
	if(action.equals(addStorageCallBack)) {
	    String add_storage  = request.getParameter("add_storage");
	    String update_years = request.getParameter("update_years");
	    String error = null;
	    
	    if(add_storage != null && add_storage.equals("Add") || update_years != null && update_years.equals("Update")) {
		String storage_id = request.getParameter("storage_id");
		if(storage_id == null) {
		    error = "Please select a package to add.";
		}
	    
		if(error == null) {
		    PaymentBean pBean = new PaymentBean();
		    ArrayList a = ubean.getSingleStoragePriceDetail(mid, storage_id);
		    if(a != null && !a.isEmpty()) { 
			String years = request.getParameter("years");
			String unit = null;
			if(years == null) {
			    years = "1";
			}
			if(years.equals("1")) {
			    unit = "year ";
			}
			else {
			    unit = "years ";
			}

			String item_name   = years+" "+unit+" "+Integer.parseInt((String)a.get(4))/1000000+" MB Storage for "+sub_dir;
			String item_number = mid+"-"+storage_id;
			double price  = Double.parseDouble((String)a.get(2));
			int duration = Integer.parseInt((String)a.get(3));
			
			String total = td.format(price*Integer.parseInt(years));
			
			String return_url  = "http://3wcenter.com"+call_path+"?mid="+mid;
			String paied_action = "add_storage";
			out.println(pBean.storageOrderSumary(call_path, mid, addStorageCallBack, item_name, 
							     item_number, price, years, total, storage_id));
			String process_payment = request.getParameter("process_payment");
			if(process_payment != null) {
			    out.println(pBean.getPayPalStoragePaymentForm(item_name, item_number, total, years, user_name, 
									  return_url, paied_action, true));
			}
			return;
		    }
		}
	    }
	    
	    Membership mship = new Membership();
	    out.println("<BR><H3>Storage Price List</H3>"+mship.listStoragePrice(call_path, mid, "1-0", error));
	    return;
	}

	//=======================================================================
	// add folder
	//=======================================================================
	if(action.equals("1-1")) {
	    if(ubean.getTotalStorageUsage(user_name, sub_dir) >= 
	       (default_storage*user_level+ubean.getAddedStorageSize(mid, user_name, sub_dir))) {
		out.println(ubean.getAddStorageOption(call_path, mid));
		return;
	    }
	    out.println(ubean.addFolder(request, call_path, mid, sub_dir, user_name, user_level));
	    return;
	}

	//=======================================================================
	// eidt folder
	//=======================================================================
	if(action != null && action.equals("1-2")) {
	    out.println(ubean.editFolder(sw, sh, request, response, mid, sub_dir, user_name));
	    return;
	}

	//=======================================================================
	// upload image file
	//=======================================================================
	
	if(action != null && action.equals("1-3")) {
	    if(ubean.getTotalStorageUsage(user_name, sub_dir)>= (default_storage+ubean.getAddedStorageSize(mid, user_name, sub_dir))) {
		out.println(ubean.getAddStorageOption(call_path, mid));
		return;
	    }
	    session.setAttribute("page_return", request.getServletPath()+"?mid="+mid+"&aid=1-3-1");
	    out.println(ubean.uploadFile(sw, sh, request, table_name, sub_dir, user_name, user_level, image_size, null));
	}

	if(action != null && action.equals("1-3-1")) {
	    if(request.getAttribute("upload_error") == null) {
		out.println(ubean.displayAndSaveFile(sw, sh, request, response, mid, sub_dir, user_name, user_level, user_ip));
	    }
	    else {
		out.println(ubean.uploadFile(sw, sh, request, table_name, sub_dir, user_name, user_level, image_size, 
		(String)request.getAttribute("upload_error")));
	    }
	}

	//==========================================================================
	// e-mail friends
	//==========================================================================

	if(action != null && action.equals("1-4")) {
	    out.println(ubean.emailFriends(sw, sh, request, mid, table_name, sub_dir, user_name, user_ip));
	    return;
	}

	//===========================================================================
	// user image display
	//===========================================================================

	if(action != null && action.equals("3")) {
	    out.println(ubean.getImageList(sw, sh, request, mid, sub_dir, user_name));
	    return;
	}

	/////////////////////////////////////////////////////////////////////////////
	// delete
	/////////////////////////////////////////////////////////////////////////////

	if(action != null && action.equals("3-2")) {
	    if(ubean.deleteFile(request, table_name, sub_dir, user_name)) {
		try{
		    RequestDispatcher rd = request.getRequestDispatcher(request.getServletPath()+"?mid="+mid+"&aid=3"); 
		    rd.forward(request,response);
		}
		catch(Exception e) {
		    System.out.println("UserHomeMangerServlet:Error:3-2"+e);
		}
	    }
	    else {
		out.println("<BR><BR><BR><H3>Error!</H3>");
	    }
	    return;
	}

	/////////////////////////////////////////////////////////////////////////////
	// edit file
	/////////////////////////////////////////////////////////////////////////////
	if(action != null && action.equals("3-3")) {
	    out.println(ubean.editAndSaveFile(sw, sh, request, mid, table_name, sub_dir, user_name));
	    return;
	}
    }
}



